package com.l2emu.gameserver.handler.admincommandhandlers;

import java.util.Collection;

import com.l2emu.Config;
import com.l2emu.gameserver.Announcements;
import com.l2emu.gameserver.handler.IAdminCommandHandler;
import com.l2emu.gameserver.instancemanager.TownManager;
import com.l2emu.gameserver.model.L2Object;
import com.l2emu.gameserver.model.L2World;
import com.l2emu.gameserver.model.actor.instance.L2PcInstance;
import com.l2emu.gameserver.model.zone.type.L2TownZone;

public class AdminTownWar implements IAdminCommandHandler
{
	private static final String[] ADMIN_COMMANDS =
	{
		"admin_townwar_start",
		"admin_townwar_end"
	};
     private L2Object _activeObject;
     public final L2Object getActiveObject()
    {
        return _activeObject;
    }
	public boolean useAdminCommand(String command, L2PcInstance activeChar)
	{
		if (command.startsWith("admin_townwar_start"))
		{
			startTW(activeChar);
		}
		if (command.startsWith("admin_townwar_end"))
		{
			endTW(activeChar);
		}
		return true;
	}
	@SuppressWarnings("deprecation")
	private void startTW(L2PcInstance activeChar)
	{
		if (Config.TW_ALL_TOWNS)
		{
			TownManager.getTown(1).setParameter("isPeaceZone", "false");
			TownManager.getTown(2).setParameter("isPeaceZone", "false");
			TownManager.getTown(3).setParameter("isPeaceZone", "false");
			TownManager.getTown(4).setParameter("isPeaceZone", "false");
			TownManager.getTown(5).setParameter("isPeaceZone", "false");
			TownManager.getTown(6).setParameter("isPeaceZone", "false");
			TownManager.getTown(7).setParameter("isPeaceZone", "false");
			TownManager.getTown(8).setParameter("isPeaceZone", "false");
			TownManager.getTown(9).setParameter("isPeaceZone", "false");
			TownManager.getTown(10).setParameter("isPeaceZone", "false");
			TownManager.getTown(11).setParameter("isPeaceZone", "false");
			TownManager.getTown(12).setParameter("isPeaceZone", "false");
			TownManager.getTown(13).setParameter("isPeaceZone", "false");
			TownManager.getTown(14).setParameter("isPeaceZone", "false");
			TownManager.getTown(15).setParameter("isPeaceZone", "false");
			TownManager.getTown(16).setParameter("isPeaceZone", "false");
			TownManager.getTown(17).setParameter("isPeaceZone", "false");
			TownManager.getTown(19).setParameter("isPeaceZone", "false");
			TownManager.getTown(20).setParameter("isPeaceZone", "false");	
	}
		if (!Config.TW_ALL_TOWNS && Config.TW_TOWN_ID != 18 && Config.TW_TOWN_ID != 21 && Config.TW_TOWN_ID != 22)
		{
			TownManager.getTown(Config.TW_TOWN_ID).setParameter("isPeaceZone", "false");
		}
		
		Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
		{
            int x,y,z;
            L2TownZone Town;
            byte zonaPaz = 1;
            
          	for (L2PcInstance onlinePlayer : pls)
               if (onlinePlayer.isOnline() == 1 )
                  {
                  	x = onlinePlayer.getX();
                  	y = onlinePlayer.getY();
                  	z = onlinePlayer.getZ();
                  
                  	Town = TownManager.getTown(x, y, z);
                  	if (Town != null)
                  	{
                  		if(Town.getTownId() == Config.TW_TOWN_ID && !Config.TW_ALL_TOWNS)
                  		{
                  			onlinePlayer.setInsideZone(zonaPaz, false);
                          		onlinePlayer.revalidateZone(true);
                  		}
                  		else if (Config.TW_ALL_TOWNS)
                  		{
                  			onlinePlayer.setInsideZone(zonaPaz, false);
                          		onlinePlayer.revalidateZone(true);
                  		}
                  	}
                  	onlinePlayer.setInTownWar(true);
                   }	
		}
		
	if (Config.TW_ALL_TOWNS)
		{
			Announcements.getInstance().announceToAll("All towns have been set war zone by " + activeChar.getName() + ".");
		}
		if (!Config.TW_ALL_TOWNS) 
		{
			Announcements.getInstance().announceToAll(TownManager.getTown(Config.TW_TOWN_ID).getName() + " has been set war zone by " + activeChar.getName() + ".");
		}
	}
	
	
	@SuppressWarnings("deprecation")
	private void endTW(L2PcInstance activeChar)
	{
		if (Config.TW_ALL_TOWNS)
		{
			TownManager.getTown(1).setParameter("isPeaceZone", "true");
			TownManager.getTown(2).setParameter("isPeaceZone", "true");
			TownManager.getTown(3).setParameter("isPeaceZone", "true");
			TownManager.getTown(4).setParameter("isPeaceZone", "true");
			TownManager.getTown(5).setParameter("isPeaceZone", "true");
			TownManager.getTown(6).setParameter("isPeaceZone", "true");
			TownManager.getTown(7).setParameter("isPeaceZone", "true");
			TownManager.getTown(8).setParameter("isPeaceZone", "true");
			TownManager.getTown(9).setParameter("isPeaceZone", "true");
			TownManager.getTown(10).setParameter("isPeaceZone", "true");
			TownManager.getTown(11).setParameter("isPeaceZone", "true");
			TownManager.getTown(12).setParameter("isPeaceZone", "true");
			TownManager.getTown(13).setParameter("isPeaceZone", "true");
			TownManager.getTown(14).setParameter("isPeaceZone", "true");
			TownManager.getTown(15).setParameter("isPeaceZone", "true");
			TownManager.getTown(16).setParameter("isPeaceZone", "true");
			TownManager.getTown(17).setParameter("isPeaceZone", "true");
			TownManager.getTown(19).setParameter("isPeaceZone", "true");
			TownManager.getTown(20).setParameter("isPeaceZone", "true");
		}
		if (!Config.TW_ALL_TOWNS && Config.TW_TOWN_ID != 18 && Config.TW_TOWN_ID != 21 && Config.TW_TOWN_ID != 22)
		{
		TownManager.getTown(Config.TW_TOWN_ID).setParameter("isPeaceZone", "true");
		}
		
		Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
		{
             int xx,yy,zz;
             L2TownZone Town;
             byte zonaPaz = 1;

            for (L2PcInstance onlinePlayer : pls)
              if (onlinePlayer.isOnline() == 1 )
                  {
                  	xx = onlinePlayer.getX();
                  	yy = onlinePlayer.getY();
                  	zz = onlinePlayer.getZ();
                  
                  	Town = TownManager.getTown(xx,yy,zz);
                  	if (Town != null)
                  	{
                  		if(Town.getTownId() == Config.TW_TOWN_ID && !Config.TW_ALL_TOWNS)
                  		{
                  			onlinePlayer.setInsideZone(zonaPaz, true);
                          		onlinePlayer.revalidateZone(true);
                  		}
                  		else if (Config.TW_ALL_TOWNS)
                  		{
                  			onlinePlayer.setInsideZone(zonaPaz, true);
                          		onlinePlayer.revalidateZone(true);
                  		}
                  	}
             	    onlinePlayer.setInTownWar(false);
                  }		
            }
		if (Config.TW_ALL_TOWNS)
		{
			Announcements.getInstance().announceToAll("All towns have been set back to normal by " + activeChar.getName() + ".");	
		}
		if (!Config.TW_ALL_TOWNS)
		{
			Announcements.getInstance().announceToAll(TownManager.getTown(Config.TW_TOWN_ID).getName() + " has been set back to normal by " + activeChar.getName() + ".");
		}
	}
	
	public String[] getAdminCommandList()
	{
		return ADMIN_COMMANDS;
	}
}